//
//  YGTabBarViewController.m
//  yuanshangyiliao
//
//  Created by icholab on 2021/3/9.
//

#import "YGTabBarViewController.h"
#import "YGTabBar.h"
#import "YGPatientHomeViewController.h"
#import "YGBaseViewController.h"
#import "YGUserCenterViewController.h"
#import "YGShoppingCartViewController.h"
#import "YGProductCenterViewController.h"
#import "YGNutritionalAdviceViewController.h"
#import "BaseNavigationController.h"

@interface YGTabBarViewController ()<UITabBarControllerDelegate>
@property(nonatomic,weak)YGTabBar *tabbar;

@end

@implementation YGTabBarViewController
//加载页面
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.delegate = self;

    [self setupTabBar];
    [self setUpItemAppear];
    [self setUpSubNav];
}
//设置点击改变字体颜色
- (void)setUpItemAppear {
    
    self.tabBar.translucent = NO;
    
    NSMutableDictionary *normalAttrs = [NSMutableDictionary dictionary];
    normalAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
//    normalAttrs[NSForegroundColorAttributeName] = HEXCOLOR(0xA9A9A9);

    NSMutableDictionary *selectedAttrs = [NSMutableDictionary dictionary];
    selectedAttrs[NSFontAttributeName] = normalAttrs[NSFontAttributeName];
    selectedAttrs[NSForegroundColorAttributeName] = UIColorFromRGB(0x37C8D7);
//获取tabbaritem外观
    UITabBarItem *item = [UITabBarItem appearance];
    [item setTitleTextAttributes:normalAttrs forState:UIControlStateNormal];
    [item setTitleTextAttributes:selectedAttrs forState:UIControlStateSelected];
    
    if (@available(iOS 10.0, *)) {
//        [[UITabBar appearance] setUnselectedItemTintColor:HEXCOLOR(0xA9A9A9)];
    }
}   
//初始化TabBar
- (void) setupTabBar {

    YGTabBar *tabbar = [[YGTabBar alloc] init];
    tabbar.myDelegate = self;
    [self setValue:tabbar forKeyPath:@"tabBar"];

    _tabbar = tabbar;
}
//tabbar设置title跟图片地址
- (void)setUpSubNav {
    
    [self setupOneChildViewController:[[YGPatientHomeViewController alloc] init]
                                title:@"首页"
                                image:@"home_bug_page_icon"
                        selectedImage:@"home_bug_page selected_icon"];

    [self setupOneChildViewController:[[YGProductCenterViewController alloc] init]
                                title:@"产品中心"
                                image:@"home_bug_product_icon"
                        selectedImage:@"home_bug_product selected_icon"];
    [self setupOneChildViewController:[[YGNutritionalAdviceViewController alloc] init]
                                title:@"营养医嘱"
                                image:@"tab_placeholder"
                        selectedImage:@"tab_placeholder"];
    [self setupOneChildViewController:[[YGShoppingCartViewController alloc] init]
                                title:@"购物车"
                                image:@"home_bug_car_icon"
                        selectedImage:@"home_bug_car selected_icon"];
    [self setupOneChildViewController:[[YGUserCenterViewController alloc] init]
                                title:@"我的"
                                image:@"home_bug_mine_icon"
                        selectedImage:@"home_bug_mine selected_icon"];

}

//对TabBar的属性进行赋值操作和选中的状态的图片切换
- (void)setupOneChildViewController:(UIViewController *)vc title:(NSString *)title image:(NSString *)image selectedImage:(NSString *)selectedImage {
    
    vc.tabBarItem.title = title;
    
    vc.tabBarItem.image = [[UIImage imageNamed:image] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    BaseNavigationController *navc = [[BaseNavigationController alloc] initWithRootViewController:vc];
    [self addChildViewController:navc];
}


- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {

    
    return YES;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
    NSInteger index = [self.childViewControllers indexOfObject:viewController];
    if (index != 2) {
        [self cancelCenterButtonSelected];
    }
//    [_tabbar setCenterButtonSelected:index == 2];
}

- (void)tabBarCenterBtnDidClick:(YGTabBar *)tabBar {
    [self setSelectedIndex:2];
   
}

- (void)cancelCenterButtonSelected {
    [_tabbar setCenterButtonSelected:NO];
}


@end
